package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.Artist;

import java.util.List;

public class ArtistListAdapter  extends  RecyclerView.Adapter<ArtistListAdapter.ArtistListHolder>{
    private List<Artist>artistList;
    private Context context;
    private BaseOnclickListener baseOnclickListener;

    public ArtistListAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Artist>artistList){
        this.artistList = artistList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ArtistListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_artist,parent,false);
        return new ArtistListHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ArtistListHolder holder, int position) {
        Artist artist = artistList.get(position);
        holder.name.setText(artist.getName());

        Glide.with(context).load(artist.getImg1v1Url())
                .centerCrop().into(holder.artist_img);


        holder.view_pager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(baseOnclickListener !=null){
                    baseOnclickListener.onclick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return artistList==null ? 0 :artistList.size();
    }

    public void setBaseOnclickListener(BaseOnclickListener baseOnclickListener){
        this.baseOnclickListener = baseOnclickListener;
    }

    class ArtistListHolder extends RecyclerView.ViewHolder {
        ImageView artist_img;
        TextView name;
        View view_pager;
        public ArtistListHolder(@NonNull View itemView) {
            super(itemView);
            artist_img = itemView.findViewById(R.id.artist_img);
            name = itemView.findViewById(R.id.name);
            view_pager = itemView.findViewById(R.id.view_pagers);
        }
    }
}
